import { getCurrentInstance, ref } from "vue"
import { useRouter, useRoute } from "vue-router"
// ant design
import { message } from "ant-design-vue"
// api
import { librarydelete, libraryon, libraryoff, librarydetailed } from "@/api/index"
export function useLibrary(params = {}) {
    const router = useRouter()
    const route = useRoute()
    // proxy
    const { proxy } = getCurrentInstance()
    // id
    const id = ref(route.params?.id)
    // useState
    const detailed = ref({})

    /**
   * 获取详情
   */
    const useGetDetailed = () => {
        librarydetailed({ id: id.value }).then(res => {
            detailed.value = res.content
        })
    }

    /**
     * 删除
     * 单一原则，一个函数只做一件事情
     */
    const useDeleteLibrary = (row) => {
        return new Promise((res, rej) => {
            proxy.$confirm({
                title: "确认删除？",
                content: "是否确认删除该藏馆",
                okAsync: () => {
                    return new Promise((resolve, reject) => { // then，catch
                        librarydelete({ id: row.id }).then(() => {
                            message.success('删除成功')
                            // useGetLibraryList()
                            // if(params.type === 'detailed') {
                            //     router.replace('/cangguan')
                            // }
                            // if(params.type === 'list') {
                            //     router.replace('/cangguan')
                            // }
                            res()
                            resolve()
                        }).catch(() => {
                            rej()
                            reject()
                        })
                    })
                }
            })
        })

    }

    /**
     * 开馆
     */
    const useOpenLibrary = (row) => {
        console.log('row', row)
        return new Promise((res, rej) => {
            proxy.$confirm({
                title: "确认开馆？",
                content: "是否确认开馆",
                okAsync: () => {
                    return new Promise((resolve, reject) => { // then，catch
                        libraryon({ id: row.id }).then(() => {
                            message.success('开馆成功')
                            //   useGetLibraryList()
                            res()
                            resolve()
                        }).catch(() => {
                            rej()
                            reject()
                        })
                    })
                }
            })
        })

    }

    /**
     * 闭馆
     */
    const useCloseLibrary = (row) => {
        return new Promise((res, rej) => {
            proxy.$confirm({
                title: "确认闭馆？",
                content: "是否确认该藏馆关闭",
                okAsync: () => {
                    return new Promise((resolve, reject) => { // then，catch
                        libraryoff({ id: row.id }).then(() => {
                            message.success('闭馆成功')
                            //   useGetLibraryList()
                            res()
                            resolve()
                        }).catch(() => {
                            rej()
                            reject()
                        })
                    })
                }
            })
        })

    }


    return {
        detailed,
        useDeleteLibrary,
        useOpenLibrary,
        useCloseLibrary,
        useGetDetailed
    }
}